//
//  GYMainTabBarController.m
//  GoodLuck
//
//  Created by apple on 2018/1/26.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainTabBarController.h"
#import "GYBaseNavigationController.h"
#import "GYHomeViewController.h"
#import "GYClassifyViewController.h"
#import "GYUser.h"

@interface GYMainTabBarController ()

/** 当前选择的tabBar标签 */
@property (nonatomic, assign) NSInteger currentIndex;
@end

@implementation GYMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showBottomTools:) name:showHomeView object:nil];
    
    //1.初始化子控制器
    [self setupChildViewControllers];
}

- (void)showBottomTools:(NSNotification *)noti {
    NSUInteger index = [noti.object integerValue];
    [self.tabBar setHidden:index == 0 ? NO : YES];
    
    //选择首页
    self.selectedIndex = index;
}

- (void)setupChildViewControllers {
    
    [self addChilderViewController:@"GYHomeViewController" image:@"home_normal" HightImage:@"home_selected" title:@"首页"];
    [self addChilderViewController:@"GYActivityViewController" image:@"home_activity_normal" HightImage:@"home_actvity_selected" title:@"活动"];
    //[self addChilderViewController:@"GYClassifyViewController" image:@"fenlei" HightImage:@"fenlei" title:@"分类"];
    //[self addChilderViewController:@"GYShopViewController" image:@"wap_sc" HightImage:@"wap_sc" title:@"店铺"];
    [self addChilderViewController:@"GYMyViewController" image:@"home_my_normal" HightImage:@"home_my_selected" title:@"我的"];
    
}

/**
 创建一个子控制器
 
 @param imageName 图片
 @param hightimage 高亮图片
 @param viewControllerName 控制器字符串名字
 @param title 标题
 */
- (void)addChilderViewController:(NSString *)viewControllerName image:(NSString *)imageName HightImage:(NSString *)hightimage title:(NSString *)title {
    
    Class viewClass = NSClassFromString(viewControllerName);
    UIViewController *viewController = [[viewClass alloc] init];
    viewController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:hightimage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.title = title;
    //设置底部文字标题的颜色 RGBColor(123, 123, 123)
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBColor(123, 123, 123)} forState:UIControlStateNormal];
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBColor(0, 158, 214)} forState:UIControlStateSelected];
    
    GYBaseNavigationController *baseNavi = [[GYBaseNavigationController alloc] initWithRootViewController:viewController];
    
    [self addChildViewController:baseNavi];

    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [self.tabBar.items indexOfObject:item];
    //1.更改控制器的时候做一些事情
    //[self.tabBar setHidden:(index == 1 || index == 2) ? YES : NO];
    
    //2.判断只要是点击第4个 没有登陆都不显示底部工具栏
    if (index == 2) {
        [self.tabBar setHidden:[GYUser loadUserInfo] == nil ? YES : NO];
        if ([GYUser loadUserInfo] == nil) {
            //应该发送一个通知给我 的界面  通知刷新界面
            [[NSNotificationCenter defaultCenter]postNotificationName:showLoginView object:nil];
        }
    }
    
    //2.添加动画
//    if (self.currentIndex != index) {
//        [self addAnimation:index];
//    }
}

//给tabbar上的图片添加动画的方法
- (void)addAnimation:(NSInteger)index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
    
    self.currentIndex = index;
}
@end
